<template>
  <div>
    <el-dialog v-model="dialogVisible" width="80%" :close-on-click-modal="false" align-center :show-close="false"
      :before-close="handleClose" destroy-on-close draggable>
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>可转化科研成果{{ ruleForm.id ? '编辑' : '新增' }}</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close">
            <CircleCloseFilled color="#fff" />
          </el-icon>
        </div>
      </template>
      <basicInfo :xlList="xlList" :zcList="zcList" :zcgsdwList="zcgsdwList" :typeIdList="typeIdList"
        :manifestationIdList="manifestationIdList" :matureIdList="matureIdList" :advancedIdList="advancedIdList"
        :domainIdList="domainIdList" :sshyList="sshyList" :obtainIdList="obtainIdList" :supportIdList="supportIdList"
        :transforIdList="transforIdList" ref="basicInfoRef" @closeDiglog="closeDiglog" @endLoading="endLoading"
        @startLoading="loading = true;">
      </basicInfo>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <el-button type="primary" @click="save(-1)" :loading="loading"
              v-if="ruleForm.checkstatus != '2'">暂存</el-button>
            <el-button type="primary" v-equalRole="['biz_kyry']" :loading="loading" @click="save(1)">提交</el-button>
            <el-button type="primary" v-if="checkEditButton()" :loading="loading" @click="save(2)">{{
      ruleForm.checkstatus == '2' ? '保存' :
        '保存并通过' }}</el-button>
            <el-button type="primary" @click="closeDiglog">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { nextTick } from 'vue'
import basicInfo from './basicInfo.vue'
import Cookies from 'js-cookie'
import useUserStore from "@/store/modules/user";
import { getPersonDetails } from "@/api/achievement/common.js"

const dialogVisible = defineModel()
const props = defineProps({
  xlList: Array,
  zcList: Array,
  zcgsdwList: Array,
  typeIdList: Array,
  manifestationIdList: Array,
  matureIdList: Array,
  advancedIdList: Array,
  domainIdList: Array,
  sshyList: Array,
  obtainIdList: Array,
  supportIdList: Array,
  transforIdList: Array,
  id: String,
})

const loading = ref(false)
const ruleForm = reactive({
  id: '',
})

const basicInfoRef = ref(null)
const getFrom = async (i) => {
  await nextTick()
  console.log('85221436547891234567890')
  console.log(basicInfoRef.value)
  for (const key in i) {
    ruleForm[key] = i[key]
  }
  basicInfoRef.value.getRuleForm(i)
}
const save = (i) => {
  basicInfoRef.value.submitForm(i)
}
const closeDiglog = () => {
  dialogVisible.value = false
  ruleForm.id = ''
  delete ruleForm.checkstatus
  emit('callBack', '')
}
const handleClick = async (name) => {
  if (name == 1) {
    await nextTick()
    supportProjectRef.value.initial(userList.value)
  }
}
const emit = defineEmits(['callBack'])
const handleClose = (done) => {
  ruleForm.id = ''
  delete ruleForm.checkstatus
  emit('callBack', '')
  done()
}
const endLoading = () => {
  loading.value = false;
}

const userStore = useUserStore();
const getKyryInfo = () => {
  if (Cookies.get("role-key") == 'biz_kyry') {
    let kyryInfo = {
      name: '',
      unitId: '',
    }
    let pamrs = {
      account: userStore.account
    }
    console.log(userStore)
    let kyryInfoArr = []
    getPersonDetails(pamrs).then(res => {
      if (res.code == 200) {
        kyryInfoArr = res.data.filter((item) => {
          return item.name == userStore.nickName
        });
        if (kyryInfoArr && kyryInfoArr.length) {
          kyryInfo.name = kyryInfoArr[0].name
          kyryInfo.unitId = kyryInfoArr[0].unitId
          kyryInfo.mobile = kyryInfoArr[0].mobile
          kyryInfo.id = kyryInfoArr[0].id
          kyryInfo.account = kyryInfoArr[0].account
          kyryInfo.titleId = kyryInfoArr[0].titleId
          kyryInfo.sexId = kyryInfoArr[0].sexId
          kyryInfo.eduLevelId = kyryInfoArr[0].eduLevelId
          kyryInfo.eduDegreeId = kyryInfoArr[0].eduDegreeId
          basicInfoRef.value.getKyryInfo(kyryInfo)
        }
      }
    })
  }
}

const checkEditButton = () => {
  if (ruleForm.id) {
    if (Cookies.get("role-key") == 'biz_kyms') {
      if (ruleForm.checkstatus == '1' && ruleForm.isAuditor) {
        return true
      } else {
        return false
      }
    } else if (Cookies.get("role-key") == 'biz_kycgly') {
      if (ruleForm.checkstatus == '1') {
        return ruleForm.isAuditor ? true : false
      } else {
        return true
      }
    } else {
      return false
    }

  } else {
    if (Cookies.get("role-key") == 'biz_kyry') {
      return false
    } else {
      return true
    }
  }
}

defineExpose({
  getFrom,
  getKyryInfo
})
</script>

<style scoped lang="scss">
@import url('@/views/achievement/index.scss');
</style>
